<?php

namespace app\shop\controller\api;

namespace app\shop\controller\sys_admin;
use app\AdminController;
use app\shop\model\WarehouseModel;
use think\Db;


/*------------------------------------------------------ */
//-- 仓库管理
/*------------------------------------------------------ */

class Warehouse extends AdminController
{
    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
        $this->Model = new WarehouseModel();
    }
    /*------------------------------------------------------ */
    //-- 主页
    /*------------------------------------------------------ */
    public function index()
    {

        $this->getList(true);

        return $this->fetch();
    }
    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false)
    {
        $where = [];
        $search['keyword'] = input('keyword', '', 'trim');
        if (empty($search['keyword']) == false) $where[] = ['title', 'like', '%' . $search['keyword'] . '%'];
        $where[] = ['is_del', '=', 0];
        $data = $this->getPageList($this->Model, $where);
        foreach ($data['list'] as $k=>$v){
            $data['list'][$k]['province']=Db::name('main_region')->where(array('id'=>$v['province']))->find()['name'];
            $data['list'][$k]['city']=Db::name('main_region')->where(array('id'=>$v['city']))->find()['name'];
            $data['list'][$k]['district']=Db::name('main_region')->where(array('id'=>$v['district']))->find()['name'];
        }
        $this->assign("data", $data);
        $this->assign("search", $search);

        if ($runData == false) {
            $data['content'] = $this->fetch('list')->getContent();
            unset($data['list']);
            return $this->success('', '', $data);
        }
        return true;
    }
    /*------------------------------------------------------ */
    //-- 详细页调用
    /*------------------------------------------------------ */
    public function asInfo($data)
    {

        if (empty($data['images']) == false) {
            $data['images'] = explode(',', $data['images']);
        }
        $shop_logistics_api=Db::name('shop_logistics_api')->where(array('isshow'=>1))->select();
        $data['shop_logistics_api']=$shop_logistics_api;
        return $data;
    }
    /*------------------------------------------------------ */
    //-- 验证数据
    /*------------------------------------------------------ */
    private function checkData($data)
    {

        return $data;
    }
    /*------------------------------------------------------ */
    //-- 添加前调用
    /*------------------------------------------------------ */
    public function beforeAdd($data)
    {
        $data['add_time'] = time();
        return $this->checkData($data);
    }
    //-- 添加后调用
    /*------------------------------------------------------ */
    public function afterAdd($data)
    {
        return $this->success('添加成功.', url('index'));
    }
    /*------------------------------------------------------ */
    //-- 修改前调用
    /*------------------------------------------------------ */
    public function beforeEdit($data)
    {
        return $this->checkData($data);
    }
    /*------------------------------------------------------ */
    //-- 修改后调用
    /*------------------------------------------------------ */
    public function afterEdit($data)
    {
        return $this->success('修改成功.');
    }
    /*------------------------------------------------------ */
    //-- 删除
    /*------------------------------------------------------ */
    public function del()
    {
        $map['id'] = input('id', 0, 'intval');
        if ($map['id'] < 1) return $this->error('传递参数失败！');
        $row = $this->Model->where($map)->find();
        $res = $this->Model->where($map)->update(array('is_del'=>1));
        if ($res < 1) return $this->error();
        $images = explode(',', $row['images']);
        foreach ($images as $img) {
            @unlink('.' . $img);
        }
        return $this->success('删除成功.');
    }
    /*------------------------------------------------------ */
    //-- 上传分享图片
    /*------------------------------------------------------ */
    public function uploadImg()
    {
        $this->returnJson = true;//统一返回json
        $result = $this->_upload($_FILES['file'], 'snap_file/');
        if ($result['error']) {
            return $this->error('上传失败，请重试.');
        }
        $file_url = str_replace('./', '/', $result['info'][0]['savepath'] . $result['info'][0]['savename']);
        $data['image'] = array('thumbnail' => $file_url, 'path' => $file_url);
        return $this->success($data);
    }
    /*------------------------------------------------------ */
    //-- 删除图片
    /*------------------------------------------------------ */
    public function removeImg()
    {
        $file = input('post.url', '', 'trim');
        unlink('.' . $file);
        return $this->success('删除成功.');
    }

}
